AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
util.PrecacheSound("geiger.wav")
function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.damaged = 0
	self.val1 = 0
	LS_RegisterEnt(self.Entity)
	
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end

	self.Entity:SetColor( 10, 96, 255, 255 )
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(250)
	end
	MakeSprite( self.Entity, "15", "100 240 100", "sprites/glow1.vmt", "2", "255")
	MakeSprite( self.Entity, "23", "250 250 250", "sprites/glow1.vmt", "1", "150")
	
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", self.Active) end
			end
		end
	end
end

function ENT:Repair()
	self.Entity:SetColor( 10, 96, 255, 255 )
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound(Sound"geiger.wav")
end


function ENT:Output()
	return 1
end

function ENT:Gen_Decay()
	local inc = 5
	RD_SupplyResource(self, "energy", inc)
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", inc) end
	return
end

function ENT:Think()
	self.Entity:EmitSound(Sound"geiger.wav")
	self.val1 = RD_GetResourceAmount(self, "energy")

	self:SetOverlayText( "Nuclear Decay Generator\nCapacitor: "..self.val1)
	
	if ( self.timer > CurTime() ) then return end
	self.Entity:EmitSound("gcounter.wav")
	self:Gen_Decay()
	self.timer = CurTime() +  1
	local dam = math.random(1,5)
	local b = ents.Create( "point_hurt" )
	b:SetKeyValue("targetname", "fire" ) 
	b:SetKeyValue("DamageRadius", "250" )
	b:SetKeyValue("Damage", tostring(dam))
	b:SetKeyValue("DamageDelay", "7" )
	b:SetKeyValue("DamageType", "262144" )
	b:SetPos( self.Entity:GetPos()+Vector(0,0,15))
	b:Spawn()
	b:Fire("turnon", "", 0)
	b:Fire("turnoff", "", 1)
	b:Fire("kill", "", 1)
	DamageLS(self.Entity, (dam/-1))
end

function MakeSprite( Entity, fx, color, spritePath, scale, transity) --thanks firec (did I speel this right? :D)
local Sprite = ents.Create("env_sprite");
Sprite:SetPos( Entity:GetPos() + Vector(0,0,15)); --just above it
Sprite:SetKeyValue( "renderfx", fx )
Sprite:SetKeyValue( "model", spritePath)
Sprite:SetKeyValue( "scale", scale)
Sprite:SetKeyValue( "spawnflags", "1")
Sprite:SetKeyValue( "angles", "0 0 0")
Sprite:SetKeyValue( "rendermode", "9")
Sprite:SetKeyValue( "renderamt", transity)
Sprite:SetKeyValue( "rendercolor", color )

Sprite:Spawn()
Sprite:Activate()
Sprite:SetParent( Entity )

end 

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
